import sys
from datetime import datetime, timedelta    # 时间类、时间差值类
def main():
    input = lambda: sys.stdin.readline().strip()
    start = datetime(1970, 1, 1, 0, 0, 0)
    for i in range(int(input())):
        parts = input().split()
        time = parts[:2]
        time = " ".join(time)
        between = int(parts[-1])
        
        # 和秒没有关系,因为从0:0:0开始,又是整数间隔分钟
        # 实例化当前时间
        now = datetime.strptime(time,"%Y-%m-%d %H:%M:%S")

        # 到目前的差值时间
        diff = now - start
        # 差了多少秒
        delta = int(diff.total_seconds()) % (60*between)
        now = now -timedelta(seconds=delta)
        print(datetime.strftime(now,"%Y-%m-%d %H:%M:%S"))

        
 
if __name__ == '__main__':
    main()